// pages/sjshangchuan/sjshangchuan.js
var app = getApp();
var ea = require('../../utils/each.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    studentid:0,
    getcurriculum: app.data.url +'/zhongxue/maoapi/getcurriculum',
    getqiniutoken: app.data.url + '/zhongxue/maoapi/getqiniutoken',
    addstudents_papers: app.data.url + '/zhongxue/maoapi/addstudents_papers',
    testpaper:[],
    xueduan:[],
    nianji:[],
    dir:"",
    name:"",
    kong:true,
    shangchuan:{},

    array:[],



    index:0,
    indexs:0,
    indexss:0,
    youhua:app.data.url +"/uploads/zhongxue/youhua.png",
    jiajia: app.data.url +"/uploads/zhongxue/jiajia.png",
    shuzu: [app.data.url +"/uploads/zhongxue/damao.png", app.data.url +"/uploads/zhongxue/damao.png", app.data.url +"/uploads/zhongxue/damao.png", app.data.url +"/uploads/zhongxue/damao.png"],
    chacha:"../../assets/newxx.png"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.id;
    this.setData({ studentid:id});
    var user = wx.getStorageSync('user');
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: that.data.getcurriculum,
      data: {token:user.token},
      header: {},
      method: 'POST',
      dataType: 'json',
      responseType: 'text',
      success: function(res) {
        var data  = res.data;
        console.log(data.data);
        if(data.code==200){
          that.setData({ array: data.data.kemu, dir: data.data.kemu[0].name, xueduan: data.data.xueduan, nianji: data.data.nianji, shangchuan: { xueduan: data.data.xueduan[0].id, nianji: data.data.nianji[0].id, dir: data.data.kemu[0].id, testpaper:[],name:''}});

        }else{
          wx.showToast({
            title: data.msg,
            icon:'none',
            duration: app.data.quanbushijian
          })
        }
      },
      fail: function(res) {},
      complete: function(res) {
        wx.hideLoading()
      },
    })
  },
  bindPickerChange1:function(e){
    var index = parseInt(e.detail.value);
    var array = this.data.xueduan;
    var shangchuan = this.data.shangchuan;
    shangchuan.xueduan = array[index].id;
    console.log(shangchuan);
    this.setData({ index: index,shangchuan:shangchuan});
  },
  bindPickerChange2: function (e) {
    var index = parseInt(e.detail.value);
    var array = this.data.nianji;
    var shangchuan = this.data.shangchuan;
    shangchuan.nianji = array[index].id;
    console.log(shangchuan);
    this.setData({ indexs: index, shangchuan: shangchuan });
  },
  bindPickerChange3: function (e) {
    var index = parseInt(e.detail.value);
    var array = this.data.array;
    var shangchuan = this.data.shangchuan;
    shangchuan.dir = array[index].id;
    console.log(shangchuan);
    this.setData({ indexss: index, shangchuan: shangchuan });
  },
  shangchuan:function(){
    var kong = this.data.kong;
    if (!kong){
      return
    }
    this.setData({kong:false})
    var tiaojian = this.data.shangchuan;
    tiaojian.student_id = this.data.studentid;
    var user  = wx.getStorageSync('user');
    tiaojian.token = user.token;
    var that = this;
    wx.showLoading({
      title: '上传中',
    })
    // console.log(tiaojian);
    // return
    wx.request({
      url: that.data.addstudents_papers,
      data: tiaojian,
      header: {},
      method: 'POST',
      dataType: 'json',
      responseType: 'text',
      success: function(res) {
        var data = res.data;
        console.log(data); 
        if(data.code==200){
          wx.showToast({
            title: '上传成功',
            duration: app.data.quanbushijian
          })
          setTimeout(function(){
            wx.navigateBack({
              delta: 1,
            })
          },1500)
        }else{
          wx.showToast({
            title: data.msg,
            icon:'none',
            duration: app.data.quanbushijian
          })
        }
      },
      fail: function(res) {},
      complete: function(res) {
        wx.hideLoading()
        that.setData({kong:true});
      },
    })
  },
  shanchu:function(e){
    // console.log(e);
    var index = e.currentTarget.dataset.index;
    var shangchuan = this.data.shangchuan;
    var testpaper = shangchuan.testpaper;
    var newtestpaper = [];
    ea.each(testpaper,function(i,v){
      // console.log(i)
      // console.log(index);
      if(i!==index){
        newtestpaper.push(v);
      }
    })
    shangchuan.testpaper = newtestpaper;
    console.log(shangchuan)
    this.setData({ shangchuan: shangchuan})
  },
  addname:function(e){
    var str = e.detail.value;
    var shangchuan = this.data.shangchuan;
    shangchuan.name = str;
    this.setData({ shangchuan: shangchuan});
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  tianjiaimg:function(){
    var shangchuan = this.data.shangchuan;
    var that = this;
    // wx.chooseMessageFile({
    //   count:9,
    //   type:'file',
    //   success:function(e){
    //     var tempFilePaths = [];
    //     ea.each(e.tempFiles,function(i,v){
    //       tempFilePaths.push(v.path);
    //     })
    //     console.log(tempFilePaths);
    //   }
    // })
    wx.chooseImage({
      count: 9,
      success: function(res) {
        var tempFilePaths = res.tempFilePaths;
        var testpaper = shangchuan.testpaper;
        if ((testpaper.length + tempFilePaths.length)>9){
          wx.showToast({
            title: '最多只能上传9张',
            duration: app.data.quanbushijian
          });
          return;
        }else{
          ea.upfile(tempFilePaths,function(name){
            console.log(name);
            testpaper = ea.addArray(testpaper, name);
            shangchuan.testpaper = testpaper;
            console.log(shangchuan);
            that.setData({ shangchuan: shangchuan})
          },that.data.getqiniutoken)
        }
      },
      fail: function(res) {},
      complete: function(res) {},
    })

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      path: '/pages/indexfirst/indexfirst',
    }
  }
})